package com.crr.service.impl;


import com.crr.api.bean.StockDto;
import com.crr.bean.*;
import com.crr.crud.MyBaseServiceImpl;
import com.crr.mapper.*;
import com.crr.bean.Spu;
import com.crr.mapper.SkuMapper;
import com.crr.mapper.SpuMapper;

import com.crr.service.SpuService;
import com.crr.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Service
public class SpuServiceImpl extends MyBaseServiceImpl<Spu, SpuMapper> implements SpuService {
    @Autowired
    SpuMapper spuMapper;
    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SpuDetailMapper spuDetailMapper;
    @Autowired
    SkuStockMapper skuStockMapper;
    @Autowired
    AttrMapper attrMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;
    @Autowired
    SpuSkuAttrValueMapper spuSkuAttrValueMapper;
    @Autowired
    SkuStockLockMapper skuStockLockMapper;
    @Autowired
    SpuExtensionMapper spuExtensionMapper;


    @Transactional
    @Override
    public int saveSpuAndSku(Spu spu) {
        int res = spuMapper.saveSpuAndSku(spu);
        Assert.isNotSuccess(res, "保存商品信息失败");
        if (null != spu.getSkuList() & spu.getSkuList().size() > 0) {
            res = skuMapper.batchSave(spu.getSpuId(), spu.getSkuList());
            Assert.isNotSuccess(res, "保存规格信息失败");
        }
        return 1;
    }


    @Transactional
    public int saveSpu(Spu entity) {
        int res = super.saveEntity(entity);
        Assert.isNotSuccess(res, "保存商品信息失败");
        //保存产品详情
        SpuDetail detail = new SpuDetail(entity.getSpuId(), entity.getDetail());
        res = spuDetailMapper.insert(detail);
        Assert.isNotSuccess(res, "保存商品详细信息失败");
        //保存规格信息和库存
        int total = 0;//总库存
        if (null != entity.getSkuList() & entity.getSkuList().size() > 0) {

            Iterator<Sku> it = entity.getSkuList().iterator();
            if (it.hasNext()) {
                //遍历skuList集合中的单个sku对象赋值给sku
                Sku sku = it.next();
                //sku中的实际库存属性赋值给total
                total += sku.getActualStock();
                //给sku设置spuid
                sku.setSpuId(entity.getSpuId());

                //保存sku
                res = skuMapper.insert(sku);
                Assert.isNotSuccess(res, "保存规格信息失败");

                //保存skuStock
                SkuStock skuStock = new SkuStock();
                skuStock.setSkuId(sku.getSkuId());
                //初始值 实际库存与可售卖库存值一致
                skuStock.setActualStock(sku.getActualStock());
                skuStock.setStock(sku.getActualStock());
                res = skuStockMapper.insert(skuStock);
                Assert.isNotSuccess(res, "保存规格库存信息失败");
            }
        }
        //保存商品扩展
        SpuExtension extension = new SpuExtension();
        extension.setSpuId(entity.getSpuId());
        //初始值 实际库存与可售卖库存值一致
        extension.setActualStock(total);
        extension.setStock(total);
        res = spuExtensionMapper.insert(extension);
        Assert.isNotSuccess(res, "保存产品扩展信息失败");

        //保存规格属性
        if (null != entity.getAttrList() && entity.getAttrList().size() > 0) {
            //遍历规格属性
            entity.getAttrList().forEach(el -> {
                //保存
                int tmp = attrMapper.insert(el);
                Assert.isNotSuccess(tmp, "保存规格属性失败");
                //保存规格值
                if (null != el.getAttrValueList() && el.getAttrValueList().size() > 0) {
                    tmp = attrValueMapper.batchSave(el.getAttrId(), el.getAttrValueList());
                    Assert.isNotSuccess(tmp, "保存规格属性值失败");
                }

                //保存商品和规格属性 属性值之间的关系
                tmp = spuAttrValueMapper.saveSpuAttrValue(entity.getSpuId(), el.getAttrId());
                Assert.isNotSuccess(tmp, "保存商品规格属性值关系失败");

                //保存 商品  规格   sku关系
                tmp=spuSkuAttrValueMapper.saveSpuSkuAttrValue(entity.getSpuId());
                Assert.isNotSuccess(tmp, "保存商品规格sku属性值关系失败");

            });
        }
        return 1;
    }

    @Override
    public Map getDetail(Serializable id) {
        return baseMapper.getDetail(id);
    }

    @Override
    public int updateStock(List<StockDto> stockDtoList) {

        if(null==stockDtoList||stockDtoList.size()==0){
            return 0;
        }
        stockDtoList.forEach(bean->{
            //更新当前sku库存
            int i = skuStockMapper.updateStock(bean);
            Assert.isNotSuccess(i,"更新sku_id"+bean.getSku_id()+"的数量减少"+bean.getCount()+"失败");

            //更新sku锁定库存记录
            int save = skuStockLockMapper.save(bean);

            //保存spu库存
            i = spuExtensionMapper.updateStock(bean.getSpu_id(), bean.getCount());
            Assert.isNotSuccess(i,"更新spu_id"+bean.getSpu_id()+"的数量减少"+bean.getCount()+"失败");


        });
        return 1;
    }


}
